﻿// Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ...

// COMMENT: I use the hint from the book - Accumulate the sum in a variable using cycle and keep 
// the old sum until the difference between the 2 sums becomes less than the accuracy (0.001)

using System;

class SumWithAccuracy
{
    static void Main()
    {
        double sum1 = 1;
        double sum2 = 0;
        double i = 2;

        while (Math.Abs(sum1 - sum2) > 0.001)
        {
            double temp = 0;

            if (i % 2 == 0)
            {
                temp = 1 / i;
            }
            else
            {
                temp = -1 / i;
            }
            sum2 = sum1;
            sum1 = sum1 + temp;
            i++;
       }

        Console.WriteLine("The sum is {0:F3}\n", sum1);
    }
}
